﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace nessmaster
{
    public class WordChecker
    {
        private ArrayList dic = new ArrayList();
        public WordChecker(string file_name)
        {
            TextReader rdr = new StreamReader(file_name);
            string s;
            while ((s = rdr.ReadLine()) != null)
            {
                dic.Add(s);
            }
        }
        public bool chek(string s)
        {

            //checking if -ness          
            //if (!s.Contains("ness"))
              //  return false;
            // checking if not in a dictionaty
              if (!dic.Contains(s))
                return false;

            return true;
        }
        public string toness(string inp)
        {
            string res = inp;
            if (inp.EndsWith("ness"))
                return res;
            res = inp + "ness";
            if (chek(res))
                return "+"+res;
            if (inp.EndsWith("y"))
            {
                res = inp.Remove(inp.Length-1,1);
                res = res + "iness";
                if (chek(res))
                    return "+" + res;

            }
            return inp;
        }
    }


    class wordprocessor
    {
        public Hashtable wordlist = new Hashtable();
        public Hashtable categories = new Hashtable();
        private WordChecker checker = new WordChecker("dict.txt");
  
        public void read_dic()
        {
            
            //            System.Text.Encoding encoding = System.Text.Encoding.BigEndianUnicode;
            System.Text.Encoding encoding = System.Text.Encoding.Unicode;
            StreamReader r1 = new StreamReader("E:\\Roget23.txt", encoding);
            
            string s;

            string pattern = @"\[ref\](?<word>[^\]\[]*)\[/ref\]";
            Regex rx = new Regex(pattern, RegexOptions.Compiled);

             //pattern = @"^ref$";
            string category = "";
            string word="";
            bool in_category = false;
            bool in_word = false;
            while (r1.Peek() >= 0)
            {

                s = r1.ReadLine();
                    if (s.Length > 1)
                        if ((s[0]!='\t'))
                        {
                            in_word = false;
                            in_category=false;
                            if (s == s.ToUpper())
                            {
                                //we'v got category word
                                category = s;
                                in_category = true;
                                categories.Add(category, new ArrayList());
                                //w1.WriteLine(s);

                                // Regex regexPattern = new Regex(pattern);
                                // MatchCollection matches = Regex.Matches(s, pattern);
                                // foreach (Match match in matches)

                            }
                            else
                            {
                                in_word = true;
                                word = s;
                                wordlist.Add(word, "");
                                
                            }
                        }
                    if (in_category)
                    {
                        //     w1.WriteLine(match.Value);
                        //Regex regexPattern = new Regex(pattern);

                        MatchCollection matches = rx.Matches(s);


                        foreach (Match match in matches)
                        {
                            GroupCollection groups = match.Groups;
                            string found = groups["word"].Value;
                            if (((found[found.Length - 1]) > '0') && ((found[found.Length - 1]) < '9'))
                                found = found.Remove(found.Length - 1, 1);
                            //w1.WriteLine("\t" + found);
                            ArrayList mylist = (ArrayList)categories[category];
                            if (found!=found.ToUpper())
                            mylist.Add(found);
                            categories[category] = mylist;
                        }


                    }
                    try
                    {
                      

                        wordlist[word] = wordlist[word].ToString() + s + "\n";
                    }
                    catch { };
                    
             
            }
        }

        public void process()
            {

                read_dic();
                StreamWriter w1 = new StreamWriter("out.txt");
            //dict ready----------------------------------------------------
            string cat = "";

            //IDictionaryEnumerator en1 = categories.GetEnumerator();
            ArrayList al = new ArrayList(categories.Keys);
            al.Sort();
            
            foreach (object kk in al)
            {
                cat = (string)kk;
                ArrayList vals = (ArrayList)categories[kk];
                w1.WriteLine("------------------------------------------------"); 
                w1.WriteLine(cat);
                foreach (object val in vals)
                {
                    string second_level = (string)val;
                    string res = checker.toness(second_level);
                    w1.WriteLine("\t"+   res);
                    string third_level = (string)wordlist[second_level];
                    //w1.WriteLine("\t\t\t"+third_level);
                    string filtered = filter_ness(third_level);
                    w1.WriteLine(filtered);
                }

                
            }
            //while (en1.MoveNext())
            //{
                //string cat = (string)en1.Key;
                //ArrayList vals = (ArrayList)en1.Value;
                //w1.WriteLine(cat);
            //}

            w1.Close(); 
            }
            
           
        

        public string filter_ness(string src)
        {
            if (src == null) src = "";
//            string pattern = @" (?<word>[a-zA-Z]*ness)";
            string pattern = @" (?<word>[a-zA-Z]+)";
            Regex rx = new Regex(pattern, RegexOptions.Compiled);
            MatchCollection matches = rx.Matches(src);
            string result = "";
            string nessed = "";
            foreach (Match match in matches)
            {
                GroupCollection groups = match.Groups;
                string found = groups["word"].Value;
                if (((found[found.Length - 1]) > '0') && ((found[found.Length - 1]) < '9'))
                    found = found.Remove(found.Length - 1, 1);
                //w1.WriteLine("\t" + found);
                nessed = checker.toness(found);
                if (nessed.EndsWith("ness"))
                result = result + "\t\t" + nessed + "\n";
                
            }
            //return "test";
            return result;

        }

        public void read_dic2(string filename)
        {
            string s;
            //while (r1.Peek() >= 0)
            {
              //  if (s.Length > 1)
                //s = r1.ReadLine();


            }
        }

    }
}
